﻿/**
 *   Array convenience method to add stack functionality to Arrays
 *   in browsers that do not support ECMAScript v3.
 *
 *   @param object element
 *   @returns number
 */
Array.prototype.push = function (element) {
    this[this.length] = element;
    return this.length;
};

/**
 *   Array convenience method to clear membership.
 *
 *   @param object element
 *   @returns void
 */
Array.prototype.clear = function () {
    this.length = 0;
};

/**
 *   Array convenience method to remove element.
 *
 *   @param object element
 *   @returns boolean
 */
Array.prototype.remove = function (element) {
	var result = false;
	var array = [];
	for (var i = 0; i < this.length; i++) {
		if (this[i] == element) {
			result = true;
		} else {
			array.push(this[i]);
		}
	}
	this.clear();
	for (var i = 0; i < array.length; i++) {
		this.push(array[i]);
	}
	array = null;
	return result;
};
    var errorList = new Array();
   
    var finished = false;
    var table = null;
    var localizedDeleteString = "Delete";
    var localizedAdvancedString = "Advanced";
    var hiddenTextBoxName = "ctl00_ctl00_ctl00_rootContent_tabbedContent_ContentPane_txtTableXml";
    var dtc = -1;
    var dataTypesMapping = new Object();
    dataTypesMapping["bigint"] = ++dtc;
    dataTypesMapping["binary"] = ++dtc;
    dataTypesMapping["bit"] = ++dtc;
    dataTypesMapping["char"] = ++dtc;
    dataTypesMapping["datetime"] = ++dtc;
    dataTypesMapping["decimal"] = ++dtc;
    dataTypesMapping["float"] = ++dtc;
    dataTypesMapping["image"] = ++dtc;
    dataTypesMapping["int"] = ++dtc;
    dataTypesMapping["money"] = ++dtc;
    dataTypesMapping["nchar"] = ++dtc;
    dataTypesMapping["ntext"] = ++dtc;
    dataTypesMapping["numeric"] = ++dtc;
    dataTypesMapping["nvarchar"] = ++dtc;
    dataTypesMapping["real"] = ++dtc;
    dataTypesMapping["smalldatetime"] = ++dtc;
    dataTypesMapping["smallint"] = ++dtc;
    dataTypesMapping["smallmoney"] = ++dtc;
    dataTypesMapping["sysname"] = ++dtc;
    dataTypesMapping["text"] = ++dtc;
    dataTypesMapping["timestamp"] = ++dtc;
    dataTypesMapping["tinyint"] = ++dtc;
    dataTypesMapping["uniqueidentifier"] = ++dtc;
    dataTypesMapping["userdefineddatatype"] = ++dtc;
    dataTypesMapping["varbinary"] = ++dtc;
    dataTypesMapping["varchar"] = ++dtc;
    dataTypesMapping["variant"] = ++dtc;
    dataTypesMapping["xml"] = ++dtc;

    dtc = -1;
    var dataTypes = new Array();
    dataTypes[++dtc] = new DataType("bigint"            , 1, 4, 0);
    dataTypes[++dtc] = new DataType("binary"            , 0, 10,0);
    dataTypes[++dtc] = new DataType("bit"               , 1, 1, 0);
    dataTypes[++dtc] = new DataType("char"              , 0, 1, 0);
    dataTypes[++dtc] = new DataType("datetime"          , 1, 8, 0);
    dataTypes[++dtc] = new DataType("decimal"           , 1, 5, 1);
    dataTypes[++dtc] = new DataType("float"             , 1, 5, 0);
    dataTypes[++dtc] = new DataType("image"             , 1, 16,0);
    dataTypes[++dtc] = new DataType("int"               , 1, 4, 0);
    dataTypes[++dtc] = new DataType("money"             , 1, 4, 0);
    dataTypes[++dtc] = new DataType("nchar"             , 0, 20,0);
    dataTypes[++dtc] = new DataType("ntext"             , 1, 16,0);
    dataTypes[++dtc] = new DataType("numeric"           , 1, 5, 1);
    dataTypes[++dtc] = new DataType("nvarchar"          , 0, 50,0);
    dataTypes[++dtc] = new DataType("real"              , 1, 8, 0);
    dataTypes[++dtc] = new DataType("smalldatetime"     , 1, 4, 0);
    dataTypes[++dtc] = new DataType("smallint"          , 1, 2, 0);
    dataTypes[++dtc] = new DataType("smallmoney"        , 1, 2, 0);
    dataTypes[++dtc] = new DataType("sysname"           , 1, 128,0);
    dataTypes[++dtc] = new DataType("text"              , 1, 16,0);
    dataTypes[++dtc] = new DataType("timestamp"         , 1, 8, 0);
    dataTypes[++dtc] = new DataType("tinyint"           , 1, 1, 0);
    dataTypes[++dtc] = new DataType("uniqueidentifier"  , 1, 12,0);
    dataTypes[++dtc] = new DataType("userdefineddatatype", 1, 16,0);
    dataTypes[++dtc] = new DataType("varbinary"         , 0, 50,0);
    dataTypes[++dtc] = new DataType("varchar"           , 0, 50,0);
    dataTypes[++dtc] = new DataType("variant"           , 1, 16,0);
    dataTypes[++dtc] = new DataType("xml"               , 1, 16,0);

//    var tableDef = new Table("dbo", "sample");
//    tableDef.columns[0] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[1] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[2] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[3] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[4] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[5] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[6] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[7] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[8] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.columns[9] = new Column( "" , 8, 4, true, false, false, 0, 0, null, 0, 0, false, "", "" );
//    tableDef.identityColumn = null;

// Class Definitions

function DataType_getNameFromIndex(index)
{
    return dataTypes[index].name;
}

function DataType_getIndexFromName(name) {
    return dataTypesMapping[name];
}
/* DataType
** A class used to track what data types are valid as well as some rules around their use
*/
function DataType( name, fixedLength, defaultSize, needsPrecisionScale )
{
    this.name = name;
    this.fixedLength = fixedLength;
    this.defaultSize = defaultSize;
    this.needsPrecisionScale = needsPrecisionScale;
    this.getIndexFromName = DataType_getIndexFromName;
    this.getNameFromIndex = DataType_getNameFromIndex;
}    

/* Table
** A class to store the data elements of a table including schema, name and a collection of Column objects
*/    
function Table_isValid() {
    var valid = false;
    var numErrors = 0;
    for( var c = 0; c < this.validColumns; c++ ) {
        var column = this.columns[c];
        if( column != null) {
            if(column.name != "")
            {
                valid = column.isValid(true);
                if(!valid)
                {
                    errorList.push("Column: " + column.name + " is invalid");
                    numErrors++;
                }
            }
        }
    }
    if(numErrors == 0)
        return true;
    else
        return false;
}
function Table_hasIdentityColumn() { return this.identityColumn != null; }
function Table_assignIdentity( column ) {
    if( this.hasIdentityColumn )
        return false;
    else {
        this.identityColumn = column;
        return true;
    }        
}
function Table_removeIdentity() {
    this.identityColumn = null;
}

function Table( name, schema )
{
    this.schema = schema;
    this.name = name;
    this.columns = new Array();
    this.identityColumn = null;
    this.validColumns = 0;
    
    this.hasIdentityColumn = Table_hasIdentityColumn;
    this.assignIdentity = Table_assignIdentity;
    this.removeIdentity = Table_removeIdentity;
    this.isValid = Table_isValid;
}

/* Column 
** A class used to track the data elements of a column
*/
function Column_isValid(reportErrors) {
    var returnValue = false;
    var index = DataType_getIndexFromName(this.dataTypeName);
    var dt = dataTypes[DataType_getIndexFromName(this.dataTypeName)];
    if( dt != null ) {
                    
        if(  this.name != "" 
        && (dt.fixedLength || ( this.size > 0 && !dt.fixedLength ))
        && ((this.identity && this.identitySeed >= 1 && this.identityIncrement >= 1 ) || ( !this.identity && this.identitySeed == 0 && this.identityIncrement == 0 ))
        && ((this.udtSchema == "" && this.udtName == "") || (this.udtSchema != "" && this.udtName != "" ) )) {
            returnValue = true;
        }
        else
            returnValue = false;
       
        if(reportErrors == true)
        {     
            if(returnValue == false && this.name != "")
            {
                alert("Column " + this.name + " is invalid");
            }   
        }  
    }
    else {
        returnValue = false;
    }
  
    return returnValue;
}

function Column_requiresSize() {
    return !dataTypes[DataType_getIndexFromName(this.dataTypeName)].fixedLength;
}

function Column( name, dataTypeName, size, nullable, key, identity, identitySeed, identityIncrement, defaultValue, precision, scale, isRowGuid, udtSchema, udtName )
{
    this.name = name;
    this.dataTypeName = dataTypeName;
    this.size = size;
    this.nullable = nullable;
    this.key = key;
    this.identity = identity;
    this.identitySeed = identitySeed;
    this.identityIncrement = identityIncrement;
    this.defaultValue = defaultValue;
    this.precision = precision;
    this.scale = scale;
    this.isRowGuid = isRowGuid;
    this.udtSchema = udtSchema;
    this.udtName = udtName;
    
    this.isValid = Column_isValid;
    this.requiresSize = Column_requiresSize;
}

// End of Class Definitions

function LoadData(startingRow)
{
  //  alert("In LoadData " + startingRow);
   // alert(table);
    table = document.getElementById("tableStructure");
    if( table != null && table != "undefined")
    {
        for( var x = startingRow; x < tableDef.columns.length; x++ )
        {
            var tr = table.insertRow(1 + x);
            var column = tableDef.columns[x];
             
            tr.id = "tableSchemaRow" + x;
           
            CreateHtmlRowFromColumn( tr, column, x );
        }
    }
    
    var txt = document.getElementById("txtName"+startingRow)
    if( txt != null )
    {
        txt.select();
        txt.focus();
    }
    
    if( !finished )
        finished = true;

    var o = document.getElementById("chkNull0");
    if( o != null )
    {
        o.checked = tableDef.columns[0].nullable;
    }
        
}

// Control creation functions
function CreateInputControl( controlType, controlName, columnDef, rowNumber, disabled, eventName, handlerName, functionRef )
{
    var o = document.createElement("input");
    o.type = controlType;
    
    AssignNewControlProperties(o, controlName, columnDef, rowNumber, disabled, eventName, handlerName, functionRef );
    
    return o;
}

function CreateDropDownControl( controlName, columnDef, rowNumber, disabled, eventName, handlerName, functionRef )
{
    var o = document.createElement("select");
    
    AssignNewControlProperties(o, controlName, columnDef, rowNumber, disabled, eventName, handlerName, functionRef );
    return o;
}

function CreateAnchorControl(text, controlName, columnDef, rowNumber )
{
    var a = document.createElement("A");
    a.appendChild(document.createTextNode(text));

    a.id = controlName + rowNumber;
    
    a.Column = columnDef;
    a.RowNumber = rowNumber;
    //AssignNewControlProperties(a, controlName, columnDef, rowNumber, false, eventName, handlerName, functionRef );
    return a;
}

function AssignNewControlProperties( control, controlName, columnDef, rowNumber, disabled, eventName, handlerName, functionRef )
{
    control.id = controlName + rowNumber;
    control.disabled = disabled;

    control.Column = columnDef;
    control.RowNumber = rowNumber;

    // wire up the event
    control[handlerName] = functionRef;
    XBrowserAddHandler(control, eventName, handlerName);
}

function XBrowserAddHandler(target,eventName,handlerName) { 
  if ( target.addEventListener ) { 
    target.addEventListener(eventName, function(e){target[handlerName](e);}, false);
  } else if ( target.attachEvent ) { 
    target.attachEvent("on" + eventName, function(e){target[handlerName](e);});
  } else { 
    var originalHandler = target["on" + eventName]; 
    if ( originalHandler ) { 
      target["on" + eventName] = function(e){originalHandler(e);target[handlerName](e);}; 
    } else { 
      target["on" + eventName] = target[handlerName]; 
    } 
  } 
}

function FillDataTypeList(control)
{
    for( var opt = 0; opt < dataTypes.length; opt++ )
    {
        var dt = dataTypes[opt];
        
        control.appendChild( CreateOption(dt.name, dt.name == "int" ? true : false ));
    }
}

function CreateOption( name, selected )
{
    var o = document.createElement("option");
    
    o.text = name;
    o.selected = selected;
    o.innerHTML = name;
    
    return o;
}

function CreateAdvancedLink( rowNumber, columnDef )
{
    var a = CreateAnchorControl( localizedAdvancedString, "lnkAdvanced", columnDef, rowNumber );// , "click", "ClickHandler", Handle_AdvancedOnClick );
    a.href = "javascript:Handle_AdvancedOnClick(this)";
    return a;
}

function CreateDeleteLink( rowNumber, columnDef  )
{
    var a = CreateAnchorControl(localizedDeleteString, "lnkDelete", columnDef, rowNumber );//, "click", "ClickHandler", Handle_DeleteOnClick );
    a.href = "javascript:Handle_DeleteOnClick(this)";
    return a;
}

// end control creation functions

function CreateHtmlRowFromColumn( tableRow, columnDef, rowNumber )
{
    var count = -1;
    var td = tableRow.insertCell(++count);
    var isDummyRow = columnDef.name == "" && rowNumber != 0 ? true : false;

    // create key column    
    var cb = CreateInputControl( "checkbox", "chkKey", columnDef, rowNumber, isDummyRow || columnDef.nullable, "click", "ClickHandler", Handle_KeyOnClick );
    cb.checked = columnDef.key;
    td.appendChild(cb);
    
    // create id column
    td = tableRow.insertCell(++count);
    cb = CreateInputControl( "checkbox", "chkID", columnDef, rowNumber, isDummyRow || columnDef.nullable, "click", "ClickHandler", Handle_IDOnClick );
    cb.checked = columnDef.identity;
    td.appendChild(cb);
    
    // create name column
    td = tableRow.insertCell(++count);
    var t = CreateInputControl( "text", "txtName", columnDef, rowNumber, isDummyRow, "change", "ChangeHandler", Handle_NameOnChange );
    t.value = columnDef.name;
    t.size = 20;
    t.maxLength = 128;
    td.appendChild(t);
    
    // create data type column
    td = tableRow.insertCell(++count);
    var dd = CreateDropDownControl("ddDataType", columnDef, rowNumber, isDummyRow, "change", "ChangeHandler", Handle_DataTypeOnChange );
    FillDataTypeList( dd );
    dd.selectedIndex = dataTypesMapping[dd.Column.dataTypeName];
    td.appendChild(dd);
    
    // create size column
    td = tableRow.insertCell(++count);
    t = CreateInputControl( "text", "txtSize", columnDef, rowNumber, true, "change", "ChangeHandler", Handle_SizeOnChange );
    t.value = columnDef.size
    t.size = 4;
    t.maxLength = 4;
    td.appendChild(t);
    
    // create null column
    td = tableRow.insertCell(++count);
    cb = CreateInputControl("checkbox", "chkNull", columnDef, rowNumber, isDummyRow, "click", "ClickHandler", Handle_NullOnClick );
    cb.checked = true;
    
    td.appendChild(cb)
    
    // create delete column
    td = tableRow.insertCell(++count);
    td.id = "tableSchemaCell" + rowNumber + "_" + count;
    if( columnDef.isValid(false) || rowNumber == 0 ) {
        var a = CreateDeleteLink(rowNumber, columnDef);
        td.appendChild(a);
    }
    else 
    {
        td.innerHTML = "&nbsp;";
    }
    td = tableRow.insertCell(++count);
    td.id = "tableSchemaCell" + rowNumber + "_" + count;
    if( columnDef.isValid(false) || rowNumber == 0 ) {
        var a = CreateAdvancedLink(rowNumber, columnDef);
        td.appendChild(a);
    }
    else 
    {
        td.innerHTML = "&nbsp;";
    }
}

function LoadAdvancedData(rowNumber) {
    var columnDef = tableDef.columns[rowNumber];
    var dt = null;
    if( columnDef != null ) {
        var cb = document.getElementById("chkRowGuid");
        var dt = dataTypes[DataType_getIndexFromName(columnDef.dataTypeName)];
        if( cb ) {
            cb.checked = columnDef.isRowGuid;
            cb.RowNumber = rowNumber;
            cb.Column = columnDef;
        }
        
        var tb = document.getElementById("txtPrecision");
        if( tb ) {
            tb.value = columnDef.precision;
            tb.RowNumber = rowNumber;
            tb.Column = columnDef;
            
            if( dt != null ) {
                tb.disabled = !dt.needsPrecisionScale;
            }
        }
        
        tb = document.getElementById("txtScale");
        if( tb ) {
            tb.value = columnDef.scale;
            tb.RowNumbre = rowNumber;
            tb.Column = columnDef;
            
            if( dt != null ) {
                tb.disabled = !dt.needsPrecisionScale;
            }
        }

        tb = document.getElementById("txtIdentityIncrement");
        if( tb ) {
            
            if(columnDef.identity)
            {
                // if the identity inc is 0 set it to 1 
                if(columnDef.identityIncrement == 0)
                {
                    columnDef.identityIncrement = 1;
                    tb.value = 1;
                }
            }
            
            tb.value = columnDef.identityIncrement;
            tb.RowNumber = rowNumber;
            tb.Column = columnDef;
            
           
            
            tb.disabled = !columnDef.identity;
        }

        tb = document.getElementById("txtIdentitySeed");
        if( tb ) {
            
            if(columnDef.identity)
            {
                // if the identity seed is 0 set it to 1 
                if(columnDef.identitySeed == 0)
                {
                    columnDef.identitySeed = 1;
                    tb.value = 1;
                }
            }
            
            tb.value = columnDef.identitySeed;
            tb.RowNumber = rowNumber;
            tb.Column = columnDef;
            
           
            
            tb.disabled = !columnDef.identity;
        }

        tb = document.getElementById("txtUdtSchema");
        if( tb ) {
            tb.value = columnDef.udtSchema;
            tb.RowNumber = rowNumber;
            tb.Column = columnDef;
        }
        
        tb = document.getElementById("txtUdtName");
        if( tb ) {
            tb.value = columnDef.udtName;
            tb.RowNumber = rowNumber;
            tb.Column = columnDef;
        }
        
        tb = document.getElementById("txtDefaultValue");
        if( tb ) {
            tb.value = columnDef.defaultValue;
            tb.RowNumber = rowNumber;
            tb.Column = columnDef;

            var div = document.getElementById("advancedSettings");
            if( div && div.style.display != "none" ) {
                tb.select();
                tb.focus();
            }
        }
    }
}

function DeleteRow( rowNumber )
{
    var column = tableDef.columns[rowNumber];
    if( column != null ) {
        tableDef.columns.remove(rowNumber);
            
        LoadData(rowNumber);
    }
}

function AssignIdColumn( rowNumber )
{
    var cb = document.getElementById("chkID" + rowNumber );
    if( cb != null ) {
        if( cb.checked ) {
            if( idColumn != -1 ) {
                alert("invalid operation trying to assign multiple columns with the identity bit");
                return;
            }
            else {
                idColumn = rowNumber;
            }
        }
        else {
            if( idColumn != rowNumber ) {
                alert("invalid operation trying to uncheck the id column when this column isn't the identity column");
                return;
            }
            else {
                idColumn = -1;
            }
        }
        
        ToggleNull( rowNumber, cb.checked );
    }
}

function EnableRow( rowNumber )
{
    var columnDef = tableDef.columns[rowNumber];
    if( columnDef != null ) {
        var cb = document.getElementById("chkNull"+rowNumber);
        cb.disabled = false;
        cb.checked = true;
        
        var tb = document.getElementById("txtName"+rowNumber);
        tb.disabled = false;
        
        var dd = document.getElementById("ddDataType"+rowNumber);
        dd.disabled = false;
        dd.selectedIndex = DataType_getIndexFromName(columnDef.dataTypeName); // integer

        var td = document.getElementById("tableSchemaCell" + rowNumber + "_" + 7);
        if( td ) {
            var a = CreateAdvancedLink(rowNumber, columnDef);
            td.innerHTML = "";
            td.appendChild(a);
        }        
    }
    tableDef.validColumns++;
}

function UpdateRowUI(rowNumber) {
    var columnDef = tableDef.columns[rowNumber];
    
    if( columnDef.isValid(false) ) {
        var td = document.getElementById("tableSchemaCell" + rowNumber + "_" + 6);
        if( td ) {
            var a = CreateDeleteLink(rowNumber, columnDef);
            td.innerHTML = "";
            td.appendChild(a);
        }        
        EnableRow(rowNumber + 1);
    }
   
}
function setControlFocus( controlID ) {
    var o = document.getElementById(controlID);
    if( o ) {
        o.select();
        o.focus();
    }
}
// event handlers
function Handle_KeyOnClick(e)
{
    this.Column.key = this.checked;
    ToggleNull(this.RowNumber, this.checked );
    UpdateRowUI(this.RowNumber);
}

function isAdvancedSettingsVisible() {
    var div = document.getElementById("advancedSettings");
    if( div )
        return div.style.display != "none";
}

function Handle_IDOnClick(e)
{
    try{
        if( !tableDef.hasIdentityColumn() ) {
            this.select();
            this.focus();
        
            this.Column.identity = this.checked;
            tableDef.assignIdentity(this.Column);
            ToggleNull(this.RowNumber, this.checked );
            
            LoadAdvancedData(this.RowNumber);
            
            if( isAdvancedSettingsVisible())
                setControlFocus("txtIdentitySeed");
        }
        else {
            this.disabled = true;
        }
        UpdateRowUI(this.RowNumber);
    }
    catch(er)
    {
        alert(er);
    }
}

function Handle_NullOnClick(e)
{
    this.Column.nullable = this.checked;
    ToggleKeyIDCheckBoxes(this.RowNumber, this.checked);
    UpdateRowUI(this.RowNumber);
}

function Handle_NameOnChange(e)
{  
    this.Column.name = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_DataTypeOnChange(e)
{
    var dt = dataTypes[this.selectedIndex];
    this.Column.dataTypeName = dt.name;
    
    ToggleSize(this.RowNumber, true );
    
    LoadAdvancedData(this.RowNumber);

    if( isAdvancedSettingsVisible() && dt.needsPrecisionScale )
        setControlFocus("txtPrecision");
    
    UpdateRowUI(this.RowNumber);
}

function Handle_SizeOnChange(e)
{
    this.Column.size = this.value;
    if( this.Column.requiresSize() ) {
        ToggleSize(this.RowNumber, false);
    }
    UpdateRowUI(this.RowNumber);
}

function Handle_DeleteOnClick(e)
{
    var currentRow = this.RowNumber;
    DeleteRow( currentRow );
    //LoadData( currentRow );
}

function Handle_AdvancedOnClick(e)
{
    var div = document.getElementById("advancedSettings");
    if( div != null ) {
        div.style.display = "block";
        LoadAdvancedData(this.RowNumber);
    }
}

function Handle_UdtSchmaOnChange(e) {
    this.Column.udtSchema = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_UdtNameOnChange(e) {
    this.Column.udtName = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_IdentitySeedOnChange(e) {
    this.Column.identitySeed = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_IdentityIncrementOnChange(e) {
    this.Column.identityIncrement = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_RowGuidOnChange(e) {
    this.Column.isRowGuid = this.checked;
    UpdateRowUI(this.RowNumber);
}

function Handle_DefaultValueOnChange(e) {
    this.Column.defaultValue = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_PrecisionOnChange(e) {
    this.Column.precision = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_ScaleOnChange(e) {
    this.Column.scale = this.value;
    UpdateRowUI(this.RowNumber);
}

function Handle_PreSubmit(e) {
    
    var errorStr = "";
    
    if(tableDef == null)
        errorStr = errorStr + "tableDef is null\n";
      
    if( tableDef != null && tableDef.isValid()) {
        // get the table Name
        var tableNameText = document.getElementById(tableNameTextBoxControlID);
        if(tableNameText != null)
        {
            tableDef.name = tableNameText.value;
        }
    
        var xmlString = "<?xml version=\"1.0\" ?>";
        xmlString += "<tableDef";
        xmlString += " name=\"" + tableDef.name + "\"";
        xmlString += " schema=\"" + tableDef.schema + "\"";
        xmlString += ">";
        
        for( var c = 0; c < tableDef.validColumns; c++ ) {
            var column = tableDef.columns[c];
            if( column != null && column.isValid(true) && column.name != "" ) {
                xmlString += "<columnDef";
                xmlString += " name=\""         + column.name           + "\"";
                xmlString += " dataTypeName=\"" + column.dataTypeName   + "\"";
                xmlString += " size=\""         + column.size           + "\"";
                xmlString += " nullable=\""     + (column.nullable ? "true" : "false") + "\"";
                xmlString += " key=\""          + (column.key ? "true" : "false") + "\"";
                xmlString += " identity=\""     + (column.identity ? "true" : "false") + "\"";
                xmlString += " identitySeed=\"" + column.identitySeed   + "\"";
                xmlString += " identityIncrement=\"" + column.identityIncrement + "\"";
                xmlString += " defaultValue=\"" + column.defaultValue   + "\"";
                xmlString += " precision=\""    + column.precision      + "\"";
                xmlString += " scale=\""        + column.scale          + "\"";
                xmlString += " isRowGuid=\""    + (column.isRowGuid ? "true" : "false") + "\"";
                xmlString += " udtSchema=\""    + column.udtSchema      + "\"";
                xmlString += " udtName=\""      + column.udtName        + "\"";
                xmlString += "/>"
            }
        }
        
        xmlString += "</tableDef>";
        
        var txt = document.getElementById(hiddenTextBoxName);
        if( txt ) {
           // alert( "Assiging value:" + xmlString);
            txt.value = xmlString;
        }
        
        return true;
    }
    else {
    
        errorStr = errorStr + "table is invalid\n"; 
        for(var i = 0; i < errorList.length;i++)
        {
            errorStr = errorStr + "--->" + errorList[i] + "\n"
        }
    
        confirm("The table definition is not in a valid format. Please correct this situation and try again\n\n Errors: " + errorStr);
        return false;
    }
}
// end event handlers

// business rules ui updates
function ToggleNull( rowNumber, disabled )
{
   // alert("ToggleNull");
    var cb = document.getElementById("chkNull" + rowNumber );
    var columnDef = tableDef.columns[rowNumber];
    
    if( cb && columnDef )
    {
        if( !columnDef.identity && !columnDef.key)
            cb.disabled = disabled;     
        else
            cb.disabled = true;
    }
}

function ToggleKeyIDCheckBoxes( rowNumber, nullChecked )
{
  //  alert("ToggleKeyIDCheckBoxes");
    var key = document.getElementById("chkKey" + rowNumber );
    var columnDef = tableDef[rowNumber];
    
    if( key != null ) {
        key.disabled = nullChecked;
    }
    
    var id = document.getElementById("chkID" + rowNumber );
    if( id != null ) {
        if( tableDef.hasIdentityColumn() ) {
            id.disabled = false;
        }
        else {
            id.disabled = nullChecked;
        }
    }
}

function ToggleSize( rowNumber , setDefaultSize)
{
    var tb = document.getElementById("txtSize" + rowNumber );
    var columnDef = tableDef.columns[rowNumber];
    
    if( tb != null )
    {
        tb.disabled = !columnDef.requiresSize();
        if(setDefaultSize == true)
        {
           var dt = dataTypes[DataType_getIndexFromName(columnDef.dataTypeName)];
           tb.value = dt.defaultSize;
        }
        else
        {
            tb.value = columnDef.size;
        }
    }
}
// end business rules ui update

